/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package observertest;

import java.awt.FlowLayout;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
import javax.swing.JFrame;

/**
 *
 * @author Student
 */
public class Game extends JFrame implements GameListener {
    
    
    
    private Board board;
    private placingState placeState = placingState.Default;
    private UserSettings ui = new UserSettings();
    private Queue actors = new LinkedList();

    public Board getBoard() {
        return board;
    }

    public void setBoard(Board board) {
        this.board = board;
    }
    
    //constructor
    public Game( Board board){
        this.setLayout(new FlowLayout());
        
        this.board = board;
        this.add(this.board);
        this.add(ui);
        
        this.setVisible(true);
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        
        this.board.addTileListeners(this);
        this.ui.addListener(this);
        
        this.pack();
        
        //this.loadResources();
    }
    
    public void addActorToTile( Tile tile, placingState type){
        Actor newActor = null;
        switch(type){ // this is where the 
            case Rocks:
                //System.out.println("actors.size = " + actors.size());
                newActor = new Rock(actors.size(),0,0,this);
                tile.addOccupant(newActor);
                actors.add(newActor);
                break;
            case Flowers:
        }
        newActor.setCurrentTile(tile);
        


        //actors.add(actor);
        //board.setTileActor(tileIndex, actor);
        System.out.println("addActorToTile() called");
    }
    
    @Override
    public void update(TileEvent e) {
        addActorToTile(e.getSource(), placeState);
//        System.out.println("tile " + e.getIndex() + " update called from Game object through GameListener interface");
//        board.getTile(e.getIndex()).setRock(false);
//        board.getTile(e.getIndex()).setFlower(false);
//        board.getTile(e.getIndex()).setCritter(false);
//        board.getTile(e.getIndex()).setBug(false);
//        board.getTile(e.getIndex()).setInvalid(false);
//        switch (placeState){
//            case Rocks:
//                Rock newRock = new Rock(0,0,0, this);
//                System.out.println(newRock.toString());
//                //addActorToTile(e.getIndex(), newRock);
//                board.getTile(e.getIndex()).setRock(true);
//                System.out.println("tile " + e.getIndex() + " has a rock in it");
//                break;
//            case Flowers:
//                board.getTile(e.getIndex()).setFlower(true);
//                System.out.println("tile " + e.getIndex() + " has a flower in it");
//                break;
//            case Critters:
//                board.getTile(e.getIndex()).setCritter(true);
//                System.out.println("tile " + e.getIndex() + " has a critter in it");
//                break;
//            case Bugs:
//                board.getTile(e.getIndex()).setBug(true);
//                System.out.println("tile " + e.getIndex() + " has a bug in it");
//                break;
//            default:
//                System.out.println("tile " + e.getIndex() + " has a nothing in it");
//        }
//        
//        //System.err.println(e.getNeighbors());
//        System.out.print("neighbor to tile " + e.getIndex() + " are: ");
//        for (Object o : e.getNeighbors()){
//            System.out.print(o + ", ");
//        }
    }

    @Override
    public void update(UIevent e) {
        placeState = e.getState();
        System.out.println("game placing state changed to placing " + placeState);
    }
    
    @Override
    public void intialize() {
        System.err.println("intialize called");
        this.board.generateTileNieghbors();
    }
}
